<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class AdminLogisticsController extends AdminbaseController {

        protected $m_logistics;

        function _initialize() {
            parent::_initialize();
            $this->m_logistics= D("Admin/Logistics");
        }

        /**
         * 物流公司管理
         */
        public function index(){
            $this->assign("user_model",$this->user_model);
            $this->assign("is_manager",is_manager($this->user_model));
            
            $keyword = I("request.keyword");
            $this->assign("keyword",$keyword);

            // 只检索上级物流公司
            $where['manager_id'] = 0;

            if(!empty($keyword)){
                $where['logistics_name'] = array('like',"%$keyword%");
            }
            if (session('ADMIN_ID') == '10') {
                $count=$this->m_logistics->withTrashed()->where($where)->count();
            }else{
                $count=$this->m_logistics->where($where)->count();
            }
            $page = $this->page($count, 15);
            
            $list = $this->m_logistics->where($where)
                ->order("create_time desc")
                ->limit($page->firstRow , $page->listRows);

            // 如果是管理员可以看到删除的
            if (session('ADMIN_ID') == '10') {
                $list = $list->withTrashed()->order("delete_time ASC,create_time DESC");
            }

            $list = $list->select();

            /*显示二级物流公司*/
            // $sub_list = $this->m_logistics->where(array('manager_id'=>array('neq',0)))->select();
            // foreach ($list as $key => $value) {

            // }


            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 二级物流公司管理
         */
        public function index_sub(){
            $lid = get_logistics_id()?get_logistics_id():0;
            $this->assign('lid',$lid);

            $keyword = I("request.keyword");
            $manager_id = I("request.manager_id");
            $manager_id = $manager_id?$manager_id:$lid;

            $this->assign("keyword",$keyword);
            $this->assign("manager_id",$manager_id);

            $logistics_sub_list = $this->m_logistics->where(array('manager_id'=>$lid))->select();
            $this->assign("logistics_sub_list",$logistics_sub_list);

            if ($manager_id) {
                $where['L.manager_id'] = array('eq',$manager_id);
            }else{
                // 只检索二级物流公司
                $where['L.manager_id'] = array('neq',0);
            }

            if(!empty($keyword)){
                $where['L.logistics_name'] = array('like',"%$keyword%");
            }

            if (session('ADMIN_ID') == '10') {
                $count=$this->m_logistics->withTrashed()->alias('L')->where($where)->count();
            }else{
                $count=$this->m_logistics->alias('L')->where($where)->count();
            }
            $page = $this->page($count, 15);
            
            $list = $this->m_logistics->alias('L')
                ->join('bdt_logistics Lu ON L.manager_id = Lu.id')
                ->field('L.*,Lu.logistics_name as upper_logistics_name')
                ->where($where)
                ->order("L.create_time desc")
                ->limit($page->firstRow , $page->listRows);

            $list = $list->withTrashed()->select();

            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            if ($lid) {
                $this->display('index_logistics');
            }else{
                $this->display();
            }
        }

        /**
         * 二级物流代充公司管理
         */
        public function index_subvip(){
            $lid = get_logistics_id()?get_logistics_id():0;
            $this->assign('lid',$lid);

            $keyword = I("request.keyword");
            $manager_id = I("request.manager_id");
            $manager_id = $manager_id?$manager_id:$lid;

            $this->assign("keyword",$keyword);
            $this->assign("manager_id",$manager_id);

            $logistics_sub_list = $this->m_logistics->where(array('manager_id'=>$lid))->select();
            $this->assign("logistics_sub_list",$logistics_sub_list);

            $where['L.manager_id'] = array('eq',1);

            if(!empty($keyword)){
                $where['L.logistics_name'] = array('like',"%$keyword%");
            }

            if (session('ADMIN_ID') == '10') {
                $count=$this->m_logistics->withTrashed()->alias('L')->where($where)->count();
            }else{
                $count=$this->m_logistics->alias('L')->where($where)->count();
            }
            $page = $this->page($count, 15);
            
            $list = $this->m_logistics->alias('L')
                ->join('bdt_logistics Lu ON L.manager_id = Lu.id')
                ->field('L.*,Lu.logistics_name as upper_logistics_name')
                ->where($where)
                ->order("L.create_time desc")
                ->limit($page->firstRow , $page->listRows);

            $list = $list->withTrashed()->select();

            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            if ($lid) {
                $this->display('index_logistics');
            }else{
                $this->display();
            }
        }


        // 添加物流公司
        public function add(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_logistics->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }
        // 添加物流公司
        public function add_sub(){
            $id = I("get.id",0,'intval');
            $logistics_sub_list = $this->m_logistics->where(array('manager_id'=>0))->select();
            $this->assign("logistics_sub_list",$logistics_sub_list);
            if(!empty($id)){
                $item=$this->m_logistics->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }
        // 添加物流公司
        public function add_subvip(){
            $id = I("get.id",0,'intval');
            $logistics_sub_list = $this->m_logistics->where(array('manager_id'=>0))->select();
            $this->assign("logistics_sub_list",$logistics_sub_list);
            if(!empty($id)){
                $item=$this->m_logistics->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }

        // 提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = I("post.");

                if(empty($id)){
                    $insert['create_time'] =time();
                    $insert['card_num'] = 0;
                    $result =$this->m_logistics->add($insert);
                    if($result){
                        $lid = $this->m_logistics->getLastInsID();
                        if(!empty($insert['credit_price'])){
                            D("Logistics/LogisticsBill")->insert_data($lid,$insert['credit_price'],1);
                        }
                        if(!empty($insert['give_price'])){
                            D("Logistics/LogisticsBill")->insert_data($lid,$insert['give_price'],5);
                        }
                    }
                }else{

                    $result = $this->m_logistics->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        // 删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_logistics->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }
        
        /*彻底删除就是把软删除关了，感觉理论上应该没错*/
        public function overdelete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_logistics->delete($id,true)!==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }
        }

        /*彻底删除就是把软删除关了*/
        public function restore(){
            $id = I('get.id',0,'intval');

            $result = $this->m_logistics->restore($id);

            if ($result) {
                $this->success("数据已恢复！");
            }else{
                $this->success("数据恢复失败！");
            }
        }
    }